from http.client import responses
from urllib import request
import json
from urllib import parse


# # 1,请求的url
# url = "https://httpbin.org/get?name=wang&age=18"
# # 2,发起请求,返回响应数据
# response = request.urlopen(url)
# # 3,解析数据
# if response.code == 200:
#     response_bytes = response.read()
#     response_dict = response_bytes.decode("utf-8")
#     response_json = json.loads(response_dict)
#     print(response_bytes)
#     print(response_dict)
#     print(response_json)
#     print(response_json["headers"]["User-Agent"], response_json["args"])




#  # 1,请求的url
# data = {
#     "name": "wang",
#     "age": 18
# }
# url_encode = parse.urlencode(data)
#
# url = f"https://httpbin.org/get?{url_encode}"
#
# # 1.1请求构造实例
# req = request.Request(url,headers={
# "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0"
# },method='GET')
#
# # 2,发起请求,返回响应数据
# response = request.urlopen(req)
# # 3,解析数据
# if response.code == 200:
#     response_bytes = response.read()
#     response_dict = response_bytes.decode("utf-8")
#     response_json = json.loads(response_dict)
#     print(response_json["headers"]["User-Agent"], response_json["args"])



# # 1,请求的url
# data = {
#     "age": 18
# }
# url_encode = parse.urlencode(data)
#
# url = "https://httpbin.org/post?name=w"
#
# # 1.1请求构造实例
# req = request.Request(url,data=url_encode.encode(),headers={
# "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
# "content-type":"application/x-www-form-urlencoded",
# },method='POST')
#
# # 2,发起请求,返回响应数据
# response = request.urlopen(req)
# # 3,解析数据
# if response.code == 200:
#     response_bytes = response.read()
#     response_dict = response_bytes.decode("utf-8")
#     response_json = json.loads(response_dict)
#     print(response_json,response_json["headers"]["User-Agent"], response_json["args"])



# 1,请求的url
data = {
    "age": 18
}
json_data = json.dumps(data)

url = "https://httpbin.org/post?name=w"

# 1.1请求构造实例
req = request.Request(url,data=json_data.encode(),headers={
"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
"content-type":"application/json",
},method='POST')

# 2,发起请求,返回响应数据
response = request.urlopen(req)
# 3,解析数据
if response.code == 200:
    response_bytes = response.read()
    response_dict = response_bytes.decode("utf-8")
    response_json = json.loads(response_dict)
    print(response_json,response_json["headers"]["User-Agent"], response_json["args"])

